package com.th.therp.mapper;

import com.github.pagehelper.Page;
import com.th.therp.pojo.dto.page.MaterialPageQueryDTO;
import com.th.therp.pojo.emtity.Material;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MaterialMapper {

    //新增商品
    @Insert("insert into material (barcode, vendor, name, specification, unit, category, brand, purchase_price, selling_price, status,is_deleted) values (#{barcode},#{vendor},#{name},#{specification},#{unit},#{category},#{brand},#{purchasePrice},#{sellingPrice},#{status},0)")
    void add(Material material);

    //快速分页查询
    Page<Material> page(MaterialPageQueryDTO materialPageQueryDTO);

    //动态修改商品
    void update(Material material);

    //根据id查询商品名称
    @Select("select name from material where id= #{materialId} ")
    String findMaterialNameById(Integer materialId);

    //根据id查询商品分类
    @Select("select category from material where id=#{materialId}")
    Integer findMaterialCategoryById(Integer materialId);

    //根据id查询商品品牌
    @Select("select brand from material where id=#{materialId}")
    Integer findMaterialBrandById(Integer materialId);

    //根据id查询商品编码
    @Select("select barcode from material where id=#{materialId} ")
    String findMaterialBarcodeById(Integer materialId);

    //根据供应商ID查询商品
    @Select("select * from material where vendor = #{vendorId} and status = 1 and is_deleted =0")
    List<Material> findByVendorId(Long vendorId);

    //根据商品编码查询商品
    @Select("select * from material where barcode=#{materialBarcode} and status = 1")
    Material findByBarcodeId(String materialBarcode);

    //根据Id查询商品
    @Select("select * from material where id =#{materialId}")
    Material findById(Integer materialId);

    //根据商品编码和供应商Id查询商品数量
    @Select("select count(*) from material where barcode=#{barcode} and vendor=#{vendor} and is_deleted=0")
    Integer findCountByBarcodeAndVendor(String barcode, Integer vendor);

    //根据商品名称和供应商Id查询商品数量
    @Select("select count(*) from material where name=#{name} and vendor=#{vendor} and is_deleted=0")
    Integer findCountByNameAndVendor(String name, Integer vendor);

    //根据id查询采购价
    @Select("select purchase_price from material where id = #{materialId}")
    double findPurchasePriceById(Integer materialId);

    //根据id查询销售价
    @Select("select selling_price from material where id = #{materialId}")
    double findSellingPriceById(Integer materialId);

}
